<?php
/**
 * Created by PhpStorm.
 * User: cen
 * Date: 2020/6/29
 * Time: 18:49
 */

namespace app\admin\controller;


use think\Controller;
use app\admin\model\ImageModel;
use think\facade\App;

class Image extends Controller
{
    public function index(){
        return $this->fetch();
    }

    public function tableData(){
        $list = ImageModel::paginate(10)->toArray();

        return json([
            'code' => 0,
            'msg' => '',
            'count' => $list['total'],
            'data' => $list['data'],
        ]);
    }

    public function form(){

        return $this->fetch();
    }

    public function upload(){
        $imgage = $this->request->file('file');

        //$f = ;
        $path = App::getRootPath().'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'images';
        $info = $imgage->move( $path);

        $src= '/uploads/images/'.$info->getSaveName();

        return json([
            'code' => 0,
            'msg' => '',

            'data' => [
                'src' => $src
            ],
        ]);
    }


    public function submitForm(){
        $data = $this->request->param();

        if(!isset($data['img_path'])){
            return json('确实参数',400);
        }

        if(!isset($data['type'])){
            return json('确实参数',400);
        }


        try{
            ImageModel::create([
                'img_path' => $data['img_path'],
                'type' => $data['type'],
            ]);
        }catch (\Exception $e){
            return json('系统异常',400);
        }


        return json('ok');
    }

    public function del(){
        $id = $this->request->param('id');

        $res = ImageModel::find($id);

        $res->status = 1;
        $res->save();
    }

}